//
//  SYIdentifyingCodeViewController.m
//  BSHOUYIFANG
//
//  Created by lanou3g on 15/8/20.
//  Copyright (c) 2015年 CYB. All rights reserved.
//

#import "SYIdentifyingCodeViewController.h"
#import "Header.h"
#import <SMS_SDK/SMSSDK.h>
#import "SYChangePasswordViewController.h"
@interface SYIdentifyingCodeViewController ()
@property(nonatomic,assign)NSInteger time;

@end

@implementation SYIdentifyingCodeViewController
- (void)times
{
    if (_time == 0) {
        return;
    }
    _time--;
   _timeLabel.text = [NSString stringWithFormat:@"接收短信大约需要%ld秒",_time];
    if (_time==0) {
        [_timeButton setTintColor:[UIColor redColor]];
          _timeLabel.text = @"";
        [_timeButton setTitle:@"请重新发送验证码" forState:UIControlStateNormal];

    }
}
- (void)timeButtonAction
{
    if (_time>2) {
        return;
    }
//     [SMSSDK getVerificationCodeBySMSWithPhone:_str zone:@"86" result:^(SMS_SDKError *error) {
//     }];
    [SMSSDK getVerificationCodeByMethod:0 phoneNumber:_str zone:@"86" customIdentifier:nil result:^(NSError *error) {
        
    }];
    _time = 60;
        [_timeButton setTitle:@"" forState:UIControlStateNormal];
    _timeLabel.text = [NSString stringWithFormat:@"接收短信大约需要%ld秒",_time];
}

- (void)viewDidLoad {
    [super viewDidLoad];
     _time = 60;
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    _phoneNumberLabel =[[UILabel alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT/6)];
    _phoneNumberLabel.text =[NSString stringWithFormat:@"我们已经发送了验证码到手机\n+86 %@",_str];
    _phoneNumberLabel.font = [UIFont systemFontOfSize:15];
    _phoneNumberLabel.numberOfLines =2;
    _phoneNumberLabel.textAlignment = NSTextAlignmentCenter;
    
    _inputCodeTF = [[UITextField alloc] initWithFrame:CGRectMake(SCREEN_HEIGHT/16, SCREEN_HEIGHT/4+10, SCREEN_WIDTH/1.3, SCREEN_WIDTH/10)];
    _inputCodeTF.center = CGPointMake(SCREEN_WIDTH/2, (SCREEN_WIDTH/10)/2+SCREEN_HEIGHT/4+10);
    _inputCodeTF.placeholder =@"填写验证码";
    _inputCodeTF.borderStyle = UITextBorderStyleRoundedRect;

    _timeButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_timeButton setTintColor:[UIColor grayColor]];
    _timeButton.frame = CGRectMake(0, SCREEN_HEIGHT/3,SCREEN_WIDTH , SCREEN_WIDTH/10);
    [_timeButton addTarget:self action:@selector(timeButtonAction) forControlEvents:UIControlEventTouchUpInside];
    _timeButton.titleLabel.font = [UIFont systemFontOfSize:15];
    
    _timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT/3,SCREEN_WIDTH , SCREEN_WIDTH/10)];
    _timeLabel.font = [UIFont systemFontOfSize:15];
    _timeLabel.textColor = [UIColor grayColor];
    _timeLabel.textAlignment = NSTextAlignmentCenter;
    _timeLabel.text = [NSString stringWithFormat:@"接收短信大约需要%ld秒",_time];
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(times) userInfo:nil repeats:YES];
    
    
    _nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _nextButton.frame = CGRectMake(SCREEN_WIDTH/12,SCREEN_HEIGHT/3+SCREEN_HEIGHT/10,SCREEN_WIDTH/1.2,SCREEN_WIDTH/10);
    _nextButton.backgroundColor = [UIColor redColor];
    [_nextButton addTarget:self action:@selector(nextButton:) forControlEvents:UIControlEventTouchUpInside];
    [_nextButton setTitle:@"下一步" forState:UIControlStateNormal];
    _nextButton.titleLabel.textColor =[UIColor whiteColor];
    
    [self.view addSubview:_phoneNumberLabel];
    [self.view addSubview:_inputCodeTF];
    [self.view addSubview:_nextButton];
    [self.view addSubview:_timeButton];
    [self.view addSubview:_timeLabel];
}
//提交验证码和服务器对比
- (void)nextButton:(UIButton *)sender
{
    [SMSSDK commitVerificationCode:_inputCodeTF.text phoneNumber:_str zone:@"86" result:^(NSError *error) {
        if (!error) {
            _time = 0;
            NSLog(@"验证成功");
            SYChangePasswordViewController *IdCodeVC =[[SYChangePasswordViewController alloc] init];
            IdCodeVC.str = [NSString stringWithFormat:@"用户名:%@",_str];
            [self.navigationController pushViewController:IdCodeVC animated:YES];
        }else
        {
            UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"验证码输入错误!请重新输入" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
            [alterView show];
        }

    }];


}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
